package xyz.xiaomuyu.moment_read_app;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import xyz.xiaomuyu.moment_read_app.Fragment.PersonalFragment;
import xyz.xiaomuyu.moment_read_app.Fragment.HomeFragment;
import xyz.xiaomuyu.moment_read_app.Fragment.NotificationFragment;


public class MyTabbar extends AppCompatActivity {

     private BottomNavigationView bottomNavigationView;
    private Fragment fragment_home;
    private Fragment fragment_personal;
    private Fragment fragment_notification;
    private Fragment[] fragments;
    private int lastfragment;//记录最后一个Fragment

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_tabbar);
        // listView = (ListView) findViewById(R.id.mainList);
        BottomNavigationView navigation = findViewById(R.id.nav_view);
        initFragment();
    }


    private void initFragment() {
        fragment_home = new HomeFragment();
        fragment_personal = new PersonalFragment();
        fragment_notification = new NotificationFragment();
        fragments = new Fragment[]{fragment_notification,fragment_home,fragment_personal};
        lastfragment = 0;
        getSupportFragmentManager().beginTransaction().replace(R.id.mainView, fragment_notification).show(fragment_notification).commit();
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.nav_view);

        bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
    }
  private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_home: {
                    if (lastfragment != 1) {
                        switchFragment(lastfragment, 1);
                        lastfragment = 1;

                    }


                    return true;
                }
                case R.id.navigation_personal: {

                    if (lastfragment != 2) {
                        switchFragment(lastfragment, 2);
                        lastfragment = 2;
                    }
                    return true;
                }
                case R.id.navigation_notifications: {

                    if (lastfragment != 0) {
                        switchFragment(lastfragment, 0);
                        lastfragment = 0;
                    }
                    return true;
                }
            }
            return false;
        }
    };
    //切换Fragment
    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.mainView, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }
}